package com.aaa.vegetablemarketexpress.controller;

import com.aaa.vegetablemarketexpress.entity.Dept;
import com.aaa.vegetablemarketexpress.service.DeptService;
import com.aaa.vegetablemarketexpress.util.Page;
import com.aaa.vegetablemarketexpress.util.Result;
import com.aaa.vegetablemarketexpress.util.ResultUtil;
import com.github.pagehelper.PageInfo;
import lombok.extern.log4j.Log4j2;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.List;
/**
 * @FileName: DeptController
 * @Description:
 * @Author: qxh
 * @CreateTime: 2025/9/9 14:22
 * @Version: 1.0.0
 */
@RestController
@RequestMapping("/dept")
@Log4j2
public class DeptController {

    @Resource
    private DeptService deptService;

    /**
     * 获取部门列表
     */
    @PostMapping("/list")
    public Result list(@RequestBody Dept dept) {
        log.info("开始查询部门列表");
        List<Dept> list = deptService.selectDeptList(dept);
        log.info("查询部门列表结束，共查询到{}条记录", list.size());
        return ResultUtil.success(list);
    }

    /**
     * 分页获取部门列表
     */
    @PostMapping("/listByPage")
    public Result listByPage(@RequestBody Page<Dept> page) {
        log.info("开始执行部门分页查询");
        Integer pageNo = page.getPageNo();
        Integer pageSize = page.getPageSize();

        if (pageNo == null || pageSize == null || pageNo <= 0 || pageSize <= 0) {
            return ResultUtil.failed("分页参数不正确");
        }

        com.github.pagehelper.PageHelper.startPage(pageNo, pageSize);
        List<Dept> list = deptService.selectDeptList(page.getData());
        PageInfo<Dept> pageInfo = new PageInfo<>(list);
        log.info("部门分页查询结束，共查询到{}条记录", pageInfo.getTotal());
        return ResultUtil.success(pageInfo);
    }

    /**
     * 获取所有正常状态的部门（用于下拉选择）
     */
    @GetMapping("/listAllNormal")
    public Result listAllNormal() {
        log.info("开始查询所有正常状态的部门");
        List<Dept> list = deptService.selectAllNormalDept();
        log.info("查询所有正常状态的部门结束，共查询到{}条记录", list.size());
        return ResultUtil.success(list);
    }

    /**
     * 统计部门数量
     */
    @PostMapping("/count")
    public Result count(@RequestBody Dept dept) {
        log.info("开始统计部门数量");
        int count = deptService.countDept(dept);
        log.info("统计部门数量结束，统计结果为{}", count);
        return ResultUtil.success(count);
    }

    /**
     * 获取部门详细信息
     */
    @GetMapping("/{deptId}")
    public Result getInfo(@PathVariable("deptId") Long deptId) {
        log.info("开始查询部门详细信息，部门ID: {}", deptId);
        Dept dept = deptService.selectDeptById(deptId);
        if (dept == null) {
            log.warn("未找到ID为{}的部门信息", deptId);
            return ResultUtil.failed("未找到对应的部门信息");
        }
        log.info("查询部门详细信息结束");
        return ResultUtil.success(dept);
    }

    /**
     * 新增部门
     */
    @PostMapping("add")
    public Result add(@RequestBody Dept dept) {
        log.info("开始新增部门，部门名称: {}", dept.getDeptName());
        try {
            int result = deptService.insertDept(dept);
            if (result > 0) {
                log.info("新增部门成功，部门ID: {}", dept.getDeptId());
                return ResultUtil.success("新增部门成功");
            } else {
                log.warn("新增部门失败");
                return ResultUtil.failed("新增部门失败");
            }
        } catch (Exception e) {
            log.error("新增部门时发生异常", e);
            return ResultUtil.failed("新增部门失败: " + e.getMessage());
        }
    }

    /**
     * 修改部门
     */
    @PutMapping("edit")
    public Result edit(@RequestBody Dept dept) {
        log.info("开始修改部门，部门ID: {}", dept.getDeptId());
        if (dept.getDeptId() == null) {
            return ResultUtil.failed("部门ID不能为空");
        }
        try {
            int result = deptService.updateDept(dept);
            if (result > 0) {
                log.info("修改部门成功，部门ID: {}", dept.getDeptId());
                return ResultUtil.success("修改部门成功");
            } else {
                log.warn("修改部门失败");
                return ResultUtil.failed("修改部门失败");
            }
        } catch (Exception e) {
            log.error("修改部门时发生异常", e);
            return ResultUtil.failed("修改部门失败: " + e.getMessage());
        }
    }

    /**
     * 删除部门
     */
    @DeleteMapping("/{deptId}")
    public Result remove(@PathVariable Long deptId) {
        log.info("开始删除部门，部门ID: {}", deptId);
        if (deptId == null) {
            return ResultUtil.failed("部门ID不能为空");
        }
        try {
            int result = deptService.deleteDeptById(deptId);
            if (result > 0) {
                log.info("删除部门成功，部门ID: {}", deptId);
                return ResultUtil.success("删除部门成功");
            } else {
                log.warn("删除部门失败");
                return ResultUtil.failed("删除部门失败");
            }
        } catch (Exception e) {
            log.error("删除部门时发生异常", e);
            return ResultUtil.failed("删除部门失败: " + e.getMessage());
        }
    }

    /**
     * 批量删除部门
     */
    @DeleteMapping("/batch/{deptIds}")
    public Result removeBatch(@PathVariable Long[] deptIds) {
        log.info("开始批量删除部门，部门IDs: {}", (Object) deptIds);
        if (deptIds == null || deptIds.length == 0) {
            return ResultUtil.failed("请选择要删除的部门");
        }
        try {
            int result = deptService.deleteDeptByIds(deptIds);
            if (result > 0) {
                log.info("批量删除部门成功，删除数量: {}", result);
                return ResultUtil.success("批量删除部门成功");
            } else {
                log.warn("批量删除部门失败");
                return ResultUtil.failed("批量删除部门失败");
            }
        } catch (Exception e) {
            log.error("批量删除部门时发生异常", e);
            return ResultUtil.failed("批量删除部门失败: " + e.getMessage());
        }
    }

    /**
     * 校验部门名称是否唯一
     */
    @GetMapping("/checkDeptNameUnique")
    public Result checkDeptNameUnique(@RequestBody Dept dept) {
        log.info("开始校验部门名称唯一性，部门名称: {}", dept.getDeptName());
        String result = deptService.checkDeptNameUnique(dept);
        log.info("校验部门名称唯一性结束，结果: {}", result);
        return ResultUtil.success(result);
    }

    /**
     * 更新部门状态
     */
    @PutMapping("/changeStatus")
    public Result changeStatus(@RequestBody Dept dept) {
        log.info("开始更新部门状态，部门ID: {}, 状态: {}", dept.getDeptId(), dept.getStatus());
        if (dept.getDeptId() == null) {
            return ResultUtil.failed("部门ID不能为空");
        }
        try {
            int result = deptService.updateDeptStatus(dept.getDeptId(), String.valueOf(dept.getStatus()));
            if (result > 0) {
                log.info("更新部门状态成功，部门ID: {}", dept.getDeptId());
                return ResultUtil.success("更新部门状态成功");
            } else {
                log.warn("更新部门状态失败");
                return ResultUtil.failed("更新部门状态失败");
            }
        } catch (Exception e) {
            log.error("更新部门状态时发生异常", e);
            return ResultUtil.failed("更新部门状态失败: " + e.getMessage());
        }
    }
}
